package com.af.robot.util;

import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.Charset;

@Component
public class CmdRun {


    private String readCmd(InputStream inputStream)throws Exception{
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()));
        String line=null;
        StringBuilder b=new StringBuilder();
        while ((line=br.readLine())!=null) {
            b.append(line).append("\n");
        }
        return b.toString();
    }


    public static boolean isListen(String port){
        Runtime rt = Runtime.getRuntime();
        try {
            Process p = rt.exec("cmd.exe /c netstat -ano | findstr 0.0.0.0:" + port + " | findstr LISTENING | findstr TCP");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.defaultCharset()));
            int count = 0;
            while ((br.readLine())!=null) {
                count++;
            }
            return count > 0;
        } catch (IOException e) {
            return false;
        }
    }
}
